#  This file is part of codeogre
#  Copyright (C) 2009 Johan Jordaan (www.johanjordaan.co.za)
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest
from lxml import etree
from StringIO import StringIO


def run_test(filename):
  xmlschema_doc = etree.parse(open('../fsm.xsd','rt'))
  xmlschema = etree.XMLSchema(xmlschema_doc)
  doc = etree.parse(open(filename,'rt'))
  isValid = xmlschema.validate(doc)
  log = xmlschema.error_log
  error = log.last_error
  #print error
  return isValid


class SchemaTestCases(unittest.TestCase):

  def setUp(self):
    pass

  def test_fsm_valid(self):
    self.assertEqual(True,isValid)

  def test_fsm_valid(self):
    self.assertEqual(True,run_test('fsm_valid.xml'))
     
  def test_fsm_invalid_start_state(self):
    self.assertEqual(False,run_test('fsm_invalid_start_state.xml'))
      
  def test_fsm_invalid_source(self):
    self.assertEqual(False,run_test('fsm_invalid_source.xml'))
      
  def test_fsm_invalid_destination(self):
    self.assertEqual(False,run_test('fsm_invalid_destination.xml'))
      
  def test_fsm_invalid_dupliacte_state(self):
    self.assertEqual(False,run_test('fsm_invalid_duplicate_state.xml'))
      
  def test_fsm_invalid_dupliacte_transition(self):
    self.assertEqual(False,run_test('fsm_invalid_duplicate_transition.xml'))

suite = unittest.makeSuite(SchemaTestCases)
          
if __name__ == '__main__':
  unittest.main()
